// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   BrowserCanvas.java
package ui;

import javax.microedition.lcdui.*;
import mForms.MainForms;

// Referenced classes of package ui:
//            MenuBar, MessageBox, Page, PageListener
public class BrowserCanvas extends Canvas
        implements Runnable {

    public boolean showing_tnc = false;
    public boolean cancel_sms = false;
    private MainForms mf ;

    public BrowserCanvas(Display display, MainForms mainForms) {
        mf= mainForms;
        judulContent = null;
        this.display = display;
        cur_page = null;
        setFullScreenMode(true);
        margin_top = 0;
        paint_flag = 3;
        margin_buttom = FONTSMALLBOLD.getHeight();
        try {

            imgbg = Image.createImage("/res/images/bgcontent.png");

            margin_top = 60;
            margin_buttom = 60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        margin_left = 40;
        margin_right =40;
    }

    public void paint(Graphics g) {
        int y = margin_top;
        if ((paint_flag & 3) == 3) {
            System.out.println("pant_flag = 3 ==>" + paint_flag);
            g.setColor(0);
            g.fillRect(0, 0, getWidth(), getPageHeight());
            warna = 0xf1cf1b;
            menuWarna1 = 0xf1cf1b;
            menuWarna2 = 0xffe25a;
            menuWarna3 = 0;
            menuWarna4 = 0x808080;
            menuWarna5 = 0xf1f1f1;

        }
        if ((paint_flag & 1) == 1) {
            System.out.println("pant_flag = 1 ==>" + paint_flag);

               g.drawImage(imgbg, 0, 0, Graphics.TOP|Graphics.LEFT);
                if(cur_page != null)
                {
                    g.setClip(margin_left - 8, y, getPageWidth() + 20, getPageHeight());
                    int scroll = cur_page.draw(g, margin_left, y, y + getPageHeight());
                    y = getHeight();
                   
                } else
                {
                    g.setColor(0xf0bf0a);
                    g.fillRect(0, y, getWidth(), getPageHeight());
                }
                g.setClip(0, 0, getWidth(), getHeight());
        }


        if (menuRight != null && menuRight.shown) {
            if (animate_flag > 0) {
                if (img_cur != null) {
                    g.drawImage(img_cur, 0, 0, 20);
                }
                if (img_next != null) {
                    g.drawImage(img_next, x2, y2, 20);
                }
                return;
            }
            menuRight.draw(g, 8, getHeight() - margin_buttom, getWidth() + 8, getHeight() - margin_buttom);
        }
        if (messageBox != null) {
            if (animate_flag > 0) {
                if (img_cur != null) {
                    g.drawImage(img_cur, 0, margin_top, 20);
                }
                if (img_next != null) {
                    g.drawImage(img_next, x2, y2, 20);
                }
            } else {
                messageBox.draw(g, getWidth(), getHeight() - margin_buttom);
            }
        }
        paint_flag = 0;
    }

    protected void keyPressed(int keyCode) {


        if (animate_flag > 0) {
            return;
        }
        last_keypress = System.currentTimeMillis();
        if (messageBox != null) {
            if (messageBox.keyPressed(keyCode)) {
                messageBox.stop();
                animate_flag = 545;
                (new Thread(this)).start();
            }
        } else if (menuRight != null && menuRight.shown) {
            int p = menuRight.keyPressed(keyCode);
            if (p > 0) {
                redraw(p);
            }
        } else {
            boolean consume = false;
            if (cur_page != null) {
                consume = cur_page.keyPressed(keyCode);
                if (consume) {
                    paint_flag |= 1;
                }
            }
            if (!consume) {
                if (keyCode == KEYSOFTRIGHT) {
                    if (cur_page != null && cur_page.listener != null) {
                        System.out.println("kanan");
                        mf.exitMIDlet();
                    }
                } else if (keyCode == KEYSOFTLEFT) {
                    System.out.println("kiri");
                     mf.showPage(MainForms.SURAT, null);
                }
            }
            if (consume) {
                repaint();
            }
        }
    }

    public void showPage(Page p, int anim) {
        if (animate_flag > 0) {
            return;
        }
        if (anim > 0) {
            animate_flag = anim | 0x100;
            next_page = p;
            (new Thread(this)).start();
        } else {
            paint_flag |= 3;
            cur_page = p;
            menuRightOption = cur_page.left_menu;
            if (menuRightOption instanceof MenuBar) {
                menuRight = (MenuBar) menuRightOption;
            }
            repaint();
        }
    }

    public int getPageHeight() {
        //return getHeight() - margin_top - margin_buttom;
        return 320 - margin_top - margin_buttom;
        //return getHeight();
    }

    public int getPageWidth() {
        //return getWidth() - margin_left - margin_right;
        return 240 - margin_left - margin_right;
    }

    public Page getPage() {
        return cur_page;
    }

    public final void showMessageBox(String msg, int type, PageListener listener, boolean anim) {
        if (messageBox != null) {
            messageBox.stop();
        }
        messageBox = new MessageBox(msg, getWidth() - 8, type, this, listener);
        messageBox.animated = anim;
        animate_flag = 546;
        (new Thread(this)).start();
    }

    protected void hideNotify() {
        paint_flag = 3;
    }

    public void redraw(int flag, boolean repaint) {
        paint_flag |= flag;
        if (repaint) {
            repaint();
        }
    }

    public void redraw(int flag) {
        paint_flag |= flag;
        repaint();
    }

    public void closeMessageBox() {
        if (messageBox != null) {
            messageBox.stop();
            messageBox = null;
            paint_flag |= 3;
            repaint();
        }
    }

    public static void drawColorDeg(Graphics g, int color, int color2, int x, int y, int w, int h) {
        int rgb1[] = new int[3];
        extractColor(color, rgb1);
        int rgb2[] = new int[3];
        extractColor(color2, rgb2);
        int rgb3[] = new int[3];
        int y2 = (y + h) - 1;
        for (int i = 0; i < h - 1; i++) {
            for (int j = 0; j < 3; j++) {
                rgb3[j] = ((rgb1[j] - rgb2[j]) * i) / h + rgb2[j];
            }

            g.setColor(rgb3[0], rgb3[1], rgb3[2]);
            g.drawLine(x, y2, x + w, y2);
            y2--;
        }

        g.setColor(color);
        g.drawLine(x, y, x + w, y);
    }

    private static void extractColor(int color, int rgb[]) {
        rgb[0] = color >> 16;
        rgb[1] = (color & 0xff00) >> 8;
        rgb[2] = color & 0xff;
    }

    public void run() {
        System.out.println("BrowserCanvas.run():" + Thread.currentThread());
        img_cur = null;
        int stop = 0;
        int div = 20;
        if ((animate_flag & 0x200) == 512) {
            img_cur = Image.createImage(cur_page.getWidth(), cur_page.screen_height);
            cur_page.draw(img_cur.getGraphics(), 0, 0, cur_page.screen_height);
            img_next = Image.createImage(messageBox.width, messageBox.height);
            messageBox.draw(img_next.getGraphics(), messageBox.width, messageBox.height);
            x2 = getWidth() - messageBox.width >> 1;
            y2 = getHeight();
            div = img_next.getHeight() >> 1;
            if ((animate_flag & 2) == 2) {
                stop = y2 - messageBox.height;
                div *= -1;
            } else {
                stop = y2;
                y2 -= messageBox.height;
            }
        } else if ((animate_flag & 0x400) == 1024) {
            img_cur = Image.createImage(getWidth(), getHeight());
            paint_flag = 3;
            int anim_old = animate_flag;
            animate_flag = 0;
            menuRight.shown = true;
            paint(img_cur.getGraphics());
            menuRight.shown = true;
            animate_flag = anim_old;
            //img_next = Image.createImage(menuRight.width, menuRight.height);
            img_next = img_cur;
            //menuRight.draw(img_next.getGraphics(), 0, menuRight.height, menuRight.width, menuRight.height);
            x2 = 8;
            y2 = getHeight() - margin_buttom;
            System.out.println("BrowserCanvas.run():" + img_next.getHeight());
            div = img_next.getHeight() >> 1;
            if ((animate_flag & 2) == 2) {
                stop = y2 - menuRight.height;
                div *= -1;
            } else {
                stop = y2;
                y2 -= menuRight.height;
            }
        } else {
            img_next = Image.createImage(next_page.getWidth(), next_page.screen_height);
            div = img_next.getWidth() >> 1;
            Graphics g = img_next.getGraphics();
            next_page.draw(g, 0, 0, next_page.screen_height);
            x1 = x2 = 0;
            y1 = y2 = margin_top;
            if ((animate_flag & 0x20) != 32) {
                if ((animate_flag & 2) == 2) {
                    x2 = getWidth();
                    div *= -1;
                } else {
                    x2 = 0 - getWidth();
                }
            }
        }
        do {
            if ((animate_flag & 0x20) == 32) {
                y2 += div;
                if (div >= 0 ? y2 > stop : y2 < stop) {
                    break;
                }
            } else {
                x2 += div;
                if (div >= 0 ? x2 > stop : x2 < stop) {
                    break;
                }
            }
            if (Math.abs(div) > 1) {
                div = (div * 3) / 4;
            } else {
                div = div <= 0 ? -1 : 1;
            }
            if ((animate_flag & 0x200) == 512) {
                paint_flag |= 8;
            } else if ((animate_flag & 0x400) == 1024) {
                paint_flag |= 4;
            } else {
                paint_flag |= 1;
            }
            repaint();
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
            }
        } while (true);
        if ((animate_flag & 0x200) == 512) {
            if ((animate_flag & 1) == 1) {
                messageBox = null;
            } else if (messageBox.animated) {
                messageBox.start();
            }
            paint_flag |= 8;
        } else if ((animate_flag & 0x400) == 1024) {
            paint_flag |= 4;
        } else {
            cur_page = next_page;
            next_page = null;
            menuRightOption = cur_page.left_menu;
            if (menuRightOption instanceof MenuBar) {
                menuRight = (MenuBar) menuRightOption;
            }
        }
        animate_flag = 0;
        paint_flag |= 3;
        repaint();
        img_cur = null;
        img_next = null;
    }

    protected void keyRepeated(int keyCode) {
        if (System.currentTimeMillis() - last_keypress > 100L) {
            keyPressed(keyCode);
        }
    }
    public static final int SLIDE_HORIZONTAL_IN = 16;
    public static final int SLIDE_VERTICAL = 32;
    public static final int LEFT_OR_TOP = 1;
    public static final int RIGHT_OR_BUTTOM = 2;
    private static final int ANIMATED_PAGE = 256;
    private static final int ANIMATED_MSGBOX = 512;
    private static final int ANIMATED_MENU = 1024;
    public Display display;
    public static final int PAINT_CENTER = 1;
    public static final int PAINT_ALL = 3;
    public static final int PAINT_MENUBAR = 2;
    public static final int PAINT_MENU = 4;
    public static final int PAINT_MSGBOX = 8;
    public static int KEYCENTER;
    public static int KEYUP;
    public static int KEYDOWN;
    public static int TRIGHT;
    public static int TLEFT;
    public static int KEYSOFTLEFT;
    public static int KEYSOFTRIGHT;
    public static final Font FONTSMALL = Font.getFont(64, 0, 8);
    public static final Font FONTSMALLITALIC = Font.getFont(64, 2, 8);
    public static final Font FONTSMALLBOLD = Font.getFont(64, 1, 8);
    public static final Font FONTBIG = Font.getFont(64, 0, 16);
    public static final Font FONTBIGBOLD = Font.getFont(64, 1, 16);
    Page cur_page;
    private Page next_page;
    private MenuBar menuRight;
    private Object menuRightOption;
    public MessageBox messageBox;
    private int paint_flag;
    private int margin_top;
    private int margin_buttom;
    int margin_left;
    private int margin_right;

    public static Image imgbg;

    private int animate_flag;
    private Image img_cur;
    private Image img_next;
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private long last_keypress;
    public static int getChoiceValue;
    public String judulContent;
    private int warna;
    public static int menuWarna1;
    public static int menuWarna2;
    public static int menuWarna3;
    public static int menuWarna4;
    public static int menuWarna5;
}
